package control;
import model.*;
import db.*;

import java.util.*;

/*This is the Client controller.
 * Its purpose is to give instructions towards manipulating the data concerning clients, including adding, updating, removing
 * or simply returning a client for inspection. 
 */

public class ClientCtrl 
{
	private DBClient dbClient = new DBClient();
	private DBContact dbContact = new DBContact();
	
	public ClientCtrl()
	{
		
	}
	
	// This method returns all clients in the database in the form of an ArrayList of type Client.
	
	public ArrayList<Client> listAllClients()
	{
		ArrayList<Client> allClients;
		allClients = dbClient.getAllClients(false);
		return allClients;
	}

	// This method searches for a client by the uniquely valued CVR field and returns it.
	
	public Client getClientByCvr(String cvr)
	{
		return dbClient.getClientByCvr(cvr, false);
	}
	
	// This method inserts a client in the database, the parameters being the Client object's fields.
	
	public void createClient(String cvr, String name, String address, String zipCode, String city, String phone, String gsm, String email, String contactCpr) throws Exception
	{
		Client toInsert = new Client();
		toInsert.setCvr(cvr);
		toInsert.setName(name);
		toInsert.setAddress(address);
		toInsert.setZipCode(zipCode);
		toInsert.setCity(city);
		toInsert.setPhone(phone);
		toInsert.setEmail(email);
		toInsert.setGsm(gsm);
		toInsert.setContact(dbContact.getContactCpr(contactCpr,false));
		dbClient.insertClient(toInsert);
	}
	
	// This method removes a client from the database.
	
	public boolean deleteClient(String cvr)
	{
		return dbClient.deleteClient(cvr);
	}
	
	// Updating a client is done by using this methods, with the parameters being the new values for the fields (except CVR
	// which is used to search for the client inside the Database.
	
	public int updateClient(String cvr, String name, String address, String zipCode, String city, String phone, String gsm, String email, String contactCpr)
	{
		Client toUpdate = dbClient.getClientByCvr(cvr,false);
		toUpdate.setName(name);
		toUpdate.setAddress(address);
		toUpdate.setZipCode(zipCode);
		toUpdate.setCity(city);
		toUpdate.setPhone(phone);
		toUpdate.setGsm(gsm);
		toUpdate.setEmail(email);
		toUpdate.setContact(dbContact.getContactCpr(contactCpr, false));
		return dbClient.updateClient(toUpdate); 
	}
}
